<?php
/**
 * 权限管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\content\controllers;

use app\libs\Pager;
use yii;
use app\libs\ApiControl;
use app\modules\content\models\IeltsSpeak;
use app\modules\content\models\IeltsSpeakList;
use app\libs\GetData;

class SpeakController extends ApiControl
{
    public $enableCsrfValidation = false;

    /**
     * [actionIndex description]
     * @Author don
     * @Date   2022-03-07
     * @return [type]     [description]
     */
    public function actionIndex()
    {
        $year = Yii::$app->request->get('year');
        $month = Yii::$app->request->get('month');
        $part = Yii::$app->request->get('part');
        $page = Yii::$app->request->get('page',1);
        $where = ' where 1=1';
        if ($year != false) {
            $where .= " and year=$year";
        }
        if ($month != false) {
            $where .= " and month=$month";
        }
        if ($part != false) {
            $where .= " and part=$part";
        }
        $offset = 15 * ($page - 1);
        $count = Yii::$app->db->createCommand("select count(*) as count from {{%ielts_speak}} $where")->queryOne();
        $url = "/content/speak/index?year=$year&month=$month&part=$part&page";
        $count = $count['count'];
        $page = new Pager("$url", $count, $page, 15);
        $str = $page->GetPager();
        $data = Yii::$app->db->createCommand("select * from {{%ielts_speak}} $where order by id desc limit $offset,15")->queryAll();
        return $this->render('index', ['data' => $data,'str'=>$str]);
    }

    /**
     * [actionAdd description]
     * @Author don
     * @Date   2022-03-07
     * @return [type]     [description]
     */
    public function actionAdd()
    {
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            if (empty($id)) {
                return $this->render('add');
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%ielts_speak}} where id=" . $id)->queryOne();
                return $this->render('add', ['data' => $data]);
            }

        } else {
            $IeltsSpeak = new IeltsSpeak();
            $getdata = new GetData();
            $must = array('cate_name' => '口语类别', 'part' => '部分','year'=>'年份','month'=>'月份');
            $data = $getdata->PostData($must, 'ielts_speak');
            if (empty($data['id'])) {
                $re = Yii::$app->db->createCommand()->insert("{{%ielts_speak}}", $data)->execute();
            } else {
                $re = $IeltsSpeak->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect('index');
            } else {
                echo '<script>alert("数据修改/添加失败，请重试");history.go(-1);</script>';
                die;
            }
        }

    }

    /**
     * [actionDel description]
     * @Author don
     * @Date   2022-03-07
     * @return [type]     [description]
     */
    public function actionDel()
    {
        $id = Yii::$app->request->get('id', '');
        $re = IeltsSpeak::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            echo true;
        }

    }

    /**
     * [actionList description]
     * @Author don
     * @Date   2022-03-07
     * @return [type]     [description]
     */
    public function actionList()
    {
        $pid = Yii::$app->request->get('pid');
        $where = ' where pid = '.$pid;
        $data = Yii::$app->db->createCommand("select * from {{%ielts_speak_list}} $where order by id desc")->queryAll();

        return $this->render('list', ['data' => $data, 'pid'=>$pid]);
    }

    /**
     * [actionAddList description]
     * @Author don
     * @Date   2022-03-07
     * @return [type]     [description]
     */
    public function actionAddList()
    {
        $pid = Yii::$app->request->get('pid');
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            if (empty($id)) {
                return $this->render('add_list', ['pid'=>$pid]);
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%ielts_speak_list}} where id=" . $id)->queryOne();
                return $this->render('add_list', ['data' => $data, 'pid'=>$pid]);
            }

        } else {
            $IeltsSpeakList = new IeltsSpeakList();
            $getdata = new GetData();
            $must = array('question' => '口语题目');
            $data = $getdata->PostData($must, 'ielts_speak_list');

            if (empty($data['id'])) {
                $re = Yii::$app->db->createCommand()->insert("{{%ielts_speak_list}}", $data)->execute();
            } else {
                $re = $IeltsSpeakList->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect(["/content/speak/list", 'pid'=>$data['pid']]); // todo
            } else {
                echo '<script>alert("数据修改/添加失败，请重试");history.go(-1);</script>';
                die;
            }
        }
    }

    /**
     * [actionDelList description]
     * @Author don
     * @Date   2022-03-07
     * @return [type]     [description]
     */
    public function actionDelList()
    {
        $id = Yii::$app->request->get('id', '');
        $re = IeltsSpeakList::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            echo true;
        }

    }
}